package com.fx.smsclient.modules.config;

import com.fx.smsclient.modules.handle.RwHandler;
import com.zx.sms.connect.manager.EndpointEntity;
import com.zx.sms.connect.manager.EndpointManager;
import com.zx.sms.connect.manager.cmpp.CMPPClientEndpointEntity;
import com.zx.sms.handler.api.BusinessHandlerInterface;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


@Configuration
@Slf4j
public class CmppClientConfiguration  {

    final EndpointManager manager = EndpointManager.INS;

    @Autowired
    CmppParams cmppParams;

    private CMPPClientEndpointEntity client = new CMPPClientEndpointEntity();


    @PostConstruct
    private void addLocalClient(){
        String accountId= cmppParams.getUser();
        String enPassword = cmppParams.getPassword();
        CMPPClientEndpointEntity client = new CMPPClientEndpointEntity();
        client.setId(accountId);
        client.setChartset(Charset.forName(cmppParams.getChartSet()));
        client.setUserName(accountId);
        client.setPassword(enPassword);
        client.setHost(cmppParams.getHost());
        client.setPort(cmppParams.getPort());
        client.setMaxChannels(cmppParams.getMaxChannels());
        client.setVersion(cmppParams.getVersion());
        client.setRetryWaitTimeSec(cmppParams.getRetryWaitTimeSec());
        client.setUseSSL(false);
        client.setReSendFailMsg(false);
        client.setMaxRetryCnt((short) 0);
        client.setSupportLongmsg(EndpointEntity.SupportLongMessage.BOTH);
        List<BusinessHandlerInterface> clienthandlers = new ArrayList<>();
        clienthandlers.add(new RwHandler());
        client.setBusinessHandlerSet(clienthandlers);
        manager.addEndpointEntity(client);
        manager.openEndpoint(client);
        manager.startConnectionCheckTask();
    }
}
